// myself/pages/energy-detail/energyDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signEnergyCount:'',
    hasSign:false,
    myEnergyInfo:{},
    CDNPATH:app.globalData.CDNPATH,
    signShow:false,
    dateList:[],
    energyTips:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getEnergyConfig()
    this.getEnergyDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getEnergyConfig () {
    app.api.get({
      url: 'get_energy_config'
    }).then(res=>{
      this.setData({
        energyTips:res
      })
    })
  },
  getEnergyDetail () {
    app.api.get({
      url: 'get_energy_detail'
    }).then(res => {
      const info = {
        days:res.days,
        energy:res.energy
      }
      const signDateArr = res.signInInfos || []
      const date = new Date()
      let m = date.getMonth()+1
      m = m<10? '0'+m:m
      let d = date.getDate()
      d = d<10? '0'+d:d
      const dateStr = m + '.' + d
      signDateArr.forEach(v=>{
        if(v.date === dateStr && v.energy === 0){
          v.canSign = true
        }
        if(v.date === dateStr && v.energy > 0){
          this.setData({
            hasSign:true
          })
        }
      })
      console.log(signDateArr,'signDateArr');
        this.setData({
          myEnergyInfo:info,
          dateList:signDateArr
        })
    })
  },
  sureSign () {
    this.setData({
      signShow:false
    })
  },
  goSign () {
    if(this.data.hasSign){
      wx.showToast({
        title: '您已签到',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    app.api.get({
      url: 'sign_in'
    }).then(res => {
      this.setData({
        signEnergyCount:res.data,
        signShow:true,
        hasSign:true
      })
      this.getEnergyDetail()
    })
  },
  goDetail () {
    wx.navigateTo({
      url: `/myself/pages/energy-detail/energyDetail`,
    })
  }
})